package com.gmail.huxiaowi.cblog.web.freemarker;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.gmail.huxiaowi.cblog.service.IUserService;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerSupport {
	protected final Log logger = LogFactory.getLog(this.getClass());
	
    @Autowired
    protected IUserService userBiz;
    
    private static final Configuration cfg = new Configuration();
    static {
        try {
            cfg.setSettings(FreemarkerSupport.class.getResourceAsStream("/freemarker.properties"));
        }
        catch (TemplateException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static Template getTemplate(ServletContext context, String path, String name) {
        
        cfg.setServletContextForTemplateLoading(context, path);
        try {
            return cfg.getTemplate(name);
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    public static ServletContext getServletContext(HttpServletRequest req) {
        return req.getSession().getServletContext();
    }
}
